{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python3入门到精通实战特训（第19期）第8课书面作业\n",
    "\n",
    "学号：116646\n",
    "\n",
    "**作业内容：**\n",
    "\n",
    "实现一个命令行工具，接收2个参数：模块名，属性名；检查该模块名下是否有该属性名，并返回结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "实现代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import importlib\n",
    "import sys\n",
    "\n",
    "module_name = sys.argv[1]\n",
    "attr_name = sys.argv[2]\n",
    "\n",
    "try:\n",
    "    module = importlib.import_module(module_name)\n",
    "    if hasattr(module, attr_name):\n",
    "        print(f\"{module_name} module has {attr_name} attribute.\")\n",
    "    else:\n",
    "        print(f\"{module_name} module does not have {attr_name} attribute.\")\n",
    "except ImportError:\n",
    "    print(f\"{module_name} module not found.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用方式:\n",
    "\n",
    "```python\n",
    "python check_attribute.py module_name attribute_name\n",
    "```\n",
    "\n",
    "使用示例:\n",
    "\n",
    "```python\n",
    "python check_attribute.py os mkdir\n",
    "```\n",
    "\n",
    "输出结果:\n",
    "```python\n",
    "os module has mkdir attribute.\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
